using B.S.RbacData.ErrorCode;
using ContentmanagementBaseRead.API.Applications.Commands.Popularizations;
using ContentmanagementBaseRead.API.Dtos;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace ContentmanagementBaseRead.API.Controllers
{
    /// <summary>
    /// 疾病科普控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class PopularizationController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<PopularizationController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志记录器</param>
        public PopularizationController(IMediator mediator, ILogger<PopularizationController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 获取疾病科普列表信息
        /// </summary>
        /// <param name="command">查询命令</param>
        /// <returns>疾病科普列表</returns>
        [HttpGet("list")]
        public async Task<ApiPaging<List<PopularizationListDto>>> GetPopularizationList([FromQuery] GetPopularizationListCommand command)
        {
            try
            {
                _logger.LogInformation("接收到疾病科普列表查询请求，页码：{PageIndex}，每页数量：{PageSize}", command.PageIndex, command.PageSize);

                var result = await _mediator.Send(command);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "疾病科普列表查询请求处理异常");
                return new ApiPaging<List<PopularizationListDto>>
                {
                    code = ResultEnumCode.fails,
                    message = $"疾病科普列表查询请求处理异常：{ex.Message}",
                    data = new List<PopularizationListDto>(),
                    TotalCount = 0,
                    PageCount = 0,
                };
            }
        }

        /// <summary>
        /// 获取医疗科室列表
        /// </summary>
        /// <returns>医疗科室列表</returns>
        [HttpGet("departments")]
        public async Task<ApiResult<List<MedicalDepartmentDto>>> GetMedicalDepartments()
        {
            try
            {
                _logger.LogInformation("接收到获取医疗科室列表请求");

                var command = new GetMedicalDepartmentsCommand();
                var result = await _mediator.Send(command);

                _logger.LogInformation("医疗科室列表获取完成，共 {Count} 个科室", result.data?.Count ?? 0);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取医疗科室列表请求处理异常");
                return new ApiResult<List<MedicalDepartmentDto>>
                {
                    code = ResultEnumCode.fails,
                    message = $"获取医疗科室列表请求处理异常：{ex.Message}",
                    data = new List<MedicalDepartmentDto>()
                };
            }
        }

        /// <summary>
        /// 获取疾病类型列表
        /// </summary>
        /// <param name="departmentId">科室ID（可选）</param>
        /// <returns>疾病类型列表</returns>
        [HttpGet("disease-types")]
        public async Task<ApiResult<List<DiseaseTypeDto>>> GetDiseaseTypes([FromQuery] int? departmentId = null)
        {
            try
            {
                _logger.LogInformation("接收到获取疾病类型列表请求，科室ID：{DepartmentId}", departmentId);

                var command = new GetDiseaseTypesCommand { DepartmentId = departmentId };
                var result = await _mediator.Send(command);

                _logger.LogInformation("疾病类型列表获取完成，共 {Count} 个疾病类型", result.data?.Count ?? 0);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取疾病类型列表请求处理异常");
                return new ApiResult<List<DiseaseTypeDto>>
                {
                    code = ResultEnumCode.fails,
                    message = $"获取疾病类型列表请求处理异常：{ex.Message}",
                    data = new List<DiseaseTypeDto>()
                };
            }
        }

        /// <summary>
        /// 获取科室疾病类型对应关系
        /// </summary>
        /// <returns>科室疾病类型对应关系</returns>
        [HttpGet("department-disease-types")]
        public async Task<ApiResult<List<DepartmentDiseaseTypeDto>>> GetDepartmentDiseaseTypes()
        {
            try
            {
                _logger.LogInformation("接收到获取科室疾病类型对应关系请求");

                var command = new GetDepartmentDiseaseTypesCommand();
                var result = await _mediator.Send(command);

                _logger.LogInformation("科室疾病类型对应关系获取完成，共 {Count} 个科室", result.data?.Count ?? 0);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取科室疾病类型对应关系请求处理异常");
                return new ApiResult<List<DepartmentDiseaseTypeDto>>
                {
                    code = ResultEnumCode.fails,
                    message = $"获取科室疾病类型对应关系请求处理异常：{ex.Message}",
                    data = new List<DepartmentDiseaseTypeDto>()
                };
            }
        }
    }
}